package com.czl.module_storehouse.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.view.View;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.utils.NumberUtils;
import com.czl.module_storehouse.R;

import java.util.List;

/**
 * @author xubin
 */
public class ViewStoreProjectItemAdapter extends BaseQuickAdapter<SortBean, BaseViewHolder> {

    private boolean isSelectedAll;

    public ViewStoreProjectItemAdapter(int layoutResId, @Nullable List<SortBean> data) {
        super(layoutResId, data);
    }

    public void setSelectedAll(boolean selectedAll) {
        isSelectedAll = selectedAll;
        for (SortBean sortBean : getData()) {
            sortBean.setSelected(isSelectedAll);
        }
        notifyDataSetChanged();
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, SortBean item) {
        helper.setText(R.id.tv_name, item.getSortName())
                .setText(R.id.tv_model, item.getSortModel())
                .setText(R.id.tv_count, "数量：" + item.getUnInStoreNum())
                .setText(R.id.tv_price, item.getPrice())
                .setText(R.id.tv_money, "金额：" + getMoney(item));
        helper.getView(R.id.cl_root)
                .setOnClickListener((View.OnClickListener) v -> clickItem(item, helper.getLayoutPosition()));

        CheckBox checkBox = helper.getView(R.id.check_box);
        checkBox.setChecked(item.selected());
        checkBox.setOnClickListener(v -> clickItem(checkBox, item, helper.getLayoutPosition()));

        helper.itemView.setOnClickListener(v -> clickItem(checkBox, item, helper.getLayoutPosition()));
    }

    /*@Override
    protected void convertPayloads(@NonNull BaseViewHolder helper, SortBean item, @NonNull List<Object> payloads) {
        super.convertPayloads(helper, item, payloads);
        helper.setChecked(R.id.check_box, item.isSelected());
    }*/

    private String getMoney(SortBean sortBean) {
        String price = sortBean.getPrice();
        int num = 0;
        if (sortBean.getFacilitySum() == null || sortBean.getFacilitySum() == 0) {
            num = sortBean.getStorageNum() == null ? 0 : sortBean.getStorageNum();
        } else {
            num = sortBean.getFacilitySum();
        }
        if (NumberUtils.isDouble(price)) {
            return NumberUtils.decimalPoints2(Double.parseDouble(price) * num);
        }
        return "0.00";
    }

    private void clickItem(CheckBox checkBox, SortBean item, int position) {
        item.setSelected(!item.selected());
        checkBox.setChecked(item.selected());
//        setViewData(position, "selected");
        if (getOnItemChildClickListener() != null) {
            getOnItemChildClickListener().onItemChildClick(this, checkBox, position);
        }
    }

    public void clickItem(SortBean item, int position) {
        item.setSelected(!item.selected());
//        setViewData(position, "selected");
        /*if (getOnItemChildClickListener() != null) {
            getOnItemChildClickListener().onItemChildClick(this, null, position);
        }*/
    }

    public int getTotalSelectedNum() {
        if (getData().isEmpty()) {
            return 0;
        }
        int total = 0;
        for (SortBean sortBean : getData()) {
            if (sortBean.selected()) {
                total++;
            }
        }
        return total;
    }
}
